
package org.itboys.mobile.service.mysql.web.promotion;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.promotion.SundayCouponActivityMapper;
import org.itboys.mobile.dao.promotion.SundayCouponMapper;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.promotion.SundayCoupon;
import org.itboys.mobile.entity.mysql.promotion.SundayCouponActivity;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * 作者：zzq
 * 时间：2017年5月15日
 * 描述：圣代_促销管理_后台_service。含多种促销。当前只有优惠券促销
 */

@Service
public class SundayPromotionService {

    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private SundayCouponActivityMapper couponActivityMapper;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayUploadService uploadService;
/****************************优惠券部分*******************//*
     * 新增或修改优惠券活动
     * @param activity
     */
    public void saveActivity(SundayCouponActivity activity,MultipartFile imageFile) throws Exception{
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
            activity.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(activity.getId()==0l){
            couponActivityMapper.insert(activity);
        }else{
            couponActivityMapper.update(activity);
        }
    }


    /**
     * 分页查询优惠券活动
     * @param request
     * @return
     */

    public PageResult<SundayCouponActivity> selectCouponActivity(HttpServletRequest request){
            Map<String,Object> param= ParamUtil.packageMysqlPageParam(request,"status");
           Serializable count = couponActivityMapper.count(param);
            return new PageResult<SundayCouponActivity>(couponActivityMapper.select(param),count==null?0l:Long.valueOf(count.toString()));
    }

    /**
     * 分页查询优惠券
     * @param request
     * @return
     */
    public PageResult<SundayCoupon> selectCoupon(HttpServletRequest request){
        Map<String,Object> param= ParamUtil.packageMysqlPageParam(request,"status","activityId");
        Serializable count = couponMapper.count(param);
        return new PageResult<SundayCoupon>(couponMapper.select(param),count==null?0l:Long.valueOf(count.toString()));
    }

    /**
     * 查找单个优惠券活动
     * @param id
     * @return
     */
    public SundayCouponActivity findOne(Long id){
        SundayCouponActivity activity=new SundayCouponActivity();
        activity.setId(0l);
        if(LongUtil.isNotZreo(id)){
            activity=couponActivityMapper.find(id);
        }
        return activity;
    }

    /**
     * 系统手动发放优惠券
     * @param memberIds
     */
    public void giveCoupon(List<Long> memberIds, Long couponActivityId){
        if(ListUtil.isNull(memberIds)) return;
        SundayCouponActivity couponActivity=couponActivityMapper.find(couponActivityId);
        Map<String,Object> param= new HashMap();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("id in",memberIds);
        List<SundayMember> members=memberService.list(param);
        Calendar calendar = Calendar.getInstance();
        Date endTime = new Date();
        endTime.setHours(0);
        endTime.setMinutes(0);
        endTime.setSeconds(0);
        calendar.setTime(endTime);
        calendar.add(calendar.DAY_OF_YEAR, couponActivity.getDay());

        for(SundayMember member:members){
            couponMapper.insert(new SundayCoupon(couponActivity.getId(),2,member.getId(),member.getNickName(), couponActivity.getBaseAmount(),
                    couponActivity.getDecuAmount(), SundayCommonConstants.coupon_status_wsy, calendar.getTime(),couponActivity.getImage()));
        }

    }
/****************************优惠券部分结束*******************/

}

